import Utils from "../common/Util";
import GameManager from "../common/GameManager";
import PopUpManager from "../common/PopUpManager";
import { GameConfig } from "../common/GameConfig";
import { LANG_ENUM, lang_zh } from "../i18n/zh";
import LangManager from "../i18n/LangManager"
import RulePanel from "./RulePanel";
import Facebook from "../Facebook/Facebook";
import EventUtils from "../gmbox/EventUtils";

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class PauseLayer extends cc.Component {
    //历史最高分
    @property(cc.Node)
    scoreNode: cc.Node;
    //继续游戏
    @property(cc.Node)
    continueBtn: cc.Node;
    //重新开始
    @property(cc.Node)
    restartBtn: cc.Node;
    //返回首页
    @property(cc.Node)
    returnBtn: cc.Node;
    //声音按钮
    @property(cc.Node)
    soundBtn: cc.Node;

    //声音按钮
    @property(cc.Node)
    soundIcon: cc.Node;
    /**声音文本 */
    @property(cc.RichText)
    soundLabel: cc.RichText
    //分享按钮
    @property(cc.Node)
    shareBtn: cc.Node;
    /**规则按钮 */
    @property(cc.Node)
    ruleBtn: cc.Node;

    //重新开始询问层
    @property(cc.Node)
    restartAskLayer: cc.Node;
    //确认重新开始按钮
    @property(cc.Node)
    confirmBtn: cc.Node;
    //取消重新开始按钮
    @property(cc.Node)
    cancelBtn: cc.Node;

    private _manager: GameManager;
    /**规则面板 */
    private _rulePanel: cc.Node;

    onLoad() {
        this._manager = GameManager.getInstance();
        this.hideRestartAskLayer(false);
        Utils.SetNodeText(this.scoreNode, this._manager.gameData.GetHeighestScore());

        this.refreshMusicBtnState();
    }

    onEnable() {
        Utils.addEventListener(this.restartBtn, this.restartBtnClicked.bind(this));
        Utils.addEventListener(this.continueBtn, this.continueBtnClicked.bind(this));
        Utils.addEventListener(this.returnBtn, this.returnBtnClicked.bind(this));
        Utils.addEventListener(this.soundBtn, this.soundBtnClicked.bind(this));
        Utils.addEventListener(this.shareBtn, this.shareBtnClicked.bind(this));
        Utils.addEventListener(this.confirmBtn, this.confirmBtnClicked.bind(this));
        Utils.addEventListener(this.cancelBtn, this.cancelBtnClicked.bind(this));
        Utils.addEventListener(this.ruleBtn, this.ruleBtnClicked.bind(this));
    }

    onDisable() {
        Utils.removeEventListener(this.restartBtn);
        Utils.removeEventListener(this.continueBtn);
        Utils.removeEventListener(this.returnBtn);
        Utils.removeEventListener(this.soundBtn);
        Utils.removeEventListener(this.shareBtn);
        Utils.removeEventListener(this.confirmBtn);
        Utils.removeEventListener(this.cancelBtn);
        Utils.removeEventListener(this.ruleBtn);
    }

    start() {
        this.showLayer();
    }

    showLayer() {
        this.node.active = true;
        this.node.scaleX = 1;
        this.node.scaleY = 1;
        PopUpManager.Show_ScaleEffect(this.node, 0.2);
    }

    removeLayer(isEffect) {
        isEffect = false;
        if (isEffect) {
            PopUpManager.Remove_ScaleEffect(this.node, 0.2);
        } else {
            if (this.node.isValid) {
                // this.node.destroy();
                this.node.removeFromParent();
            }
        }
    }

    //打开重新开始询问层
    showRestartAskLayer() {
        PopUpManager.Show_ScaleEffect(this.restartAskLayer, 0.2);
    }

    //关闭重新开始询问层
    hideRestartAskLayer(isEffect) {
        if (isEffect) {
            PopUpManager.Hide_ScaleEffect(this.restartAskLayer, 0.2);
        } else {
            this.restartAskLayer.active = false;
        }
    }

    //点击确认重新开始的按钮
    confirmBtnClicked() {
        this._manager.audioManager.PlayBtnSound();

        this.removeLayer(false);
        this.hideRestartAskLayer(false);

        this._manager.eventCenter.DispatchEvent("restart");
        EventUtils.logGameRestart("PauseLayer");
    }

    //点击取消重新开始的按钮
    cancelBtnClicked() {
        this._manager.audioManager.PlayBtnSound();

        this.hideRestartAskLayer(true);
    }

    //点击继续游戏按钮
    continueBtnClicked() {
        cc.log("continueBtnClicked");

        this._manager.audioManager.PlayBtnSound();
        this.removeLayer(true);
    }

    //点击重新开始游戏按钮
    restartBtnClicked() {
        this._manager.audioManager.PlayBtnSound();

        this.showRestartAskLayer();
    }

    //点击返回首页按钮
    returnBtnClicked() {
        this._manager.audioManager.PlayBtnSound();

        this.removeLayer(false);

        this._manager.eventCenter.DispatchEvent("go_lobby");
    }

    //点击声音按钮
    soundBtnClicked() {
        GameConfig.SoundSwitch = !GameConfig.SoundSwitch;
        if (GameConfig.SoundSwitch) {
            this._manager.audioManager.PlayBtnSound();
        }
        this.refreshMusicBtnState();
    }

    //点击分享按钮
    shareBtnClicked() {
        console.log('点击了分享按钮')
        this._manager.audioManager.PlayBtnSound();
        // Facebook.Share(null, Facebook.SHARE_IMAGE_BASE64);
        Facebook.ChooseAsync();
    }


    // // 截屏返回 iamge base6，用于 Share
    // getImgBase64() {
    //     // Getting view rect size.
    //     let visibleSize = cc.view.getVisibleSize();
    //     let width = visibleSize.width;
    //     let height = visibleSize.height;
    //     // Creating a new renderTexture.
    //     this.camera.active = true;
    //     let renderTexture = new cc.RenderTexture();
    //     let gl = cc.game._renderContext;
    //     renderTexture.initWithSize(cc.visibleRect.width, cc.visibleRect.height, gl.STENCIL_INDEX8);
    //     this.camera.targetTexture = renderTexture;
    //     // Creating a new canvas.
    //     let canvas = document.createElement('canvas');
    //     let ctx = canvas.getContext('2d');
    //     canvas.width = width;
    //     canvas.height = height;
    //     this.camera.render();
    //     // Getting pixels.
    //     let data = new Uint8Array(width * height * 4);
    //     renderTexture.readPixels(data);
    //     // Input the data into the new canvas.
    //     let rowBytes = width * 4;
    //     for (let row = 0; row < height; row++) {
    //         let srow = height - 1 - row;
    //         let data2 = new Uint8ClampedArray(data.buffer, srow * width * 4, rowBytes);
    //         let imageData = new ImageData(data2, width, 1);
    //         ctx.putImageData(imageData, 0, row);
    //     }

    //     this.camera.active = false;
    //     return canvas.toDataURL('image/png');
    // }

    /**点击规则按钮 */
    ruleBtnClicked() {
        this._manager.audioManager.PlayBtnSound();
        // PopUpManager.textTips(this.node, LangManager.getStrByKey("propTip"));
        Utils.GetPrefab("prefab/RulePanel.prefab", (prefab) => {
            if (prefab && (!this._rulePanel || !this._rulePanel.parent)) {
                this._rulePanel = cc.instantiate(prefab);
                this.node.addChild(this._rulePanel);
            }
        }, this)
    }

    refreshMusicBtnState() {
        var soundIcon = this.soundIcon.getComponent(cc.Sprite);
        if (soundIcon) {
            if (!GameConfig.SoundSwitch) {
                this._manager.audioManager.CloseSound();
                cc.loader.loadRes("startscene/close_music.png", cc.SpriteFrame, (err, spriteFrame) => {
                    soundIcon.spriteFrame = spriteFrame;
                    if (GameConfig.languages == LANG_ENUM.EN) {
                        this.soundLabel.string = `<b> ${lang_zh.sound[GameConfig.languages]} off </b>`;
                    }

                });
            } else {
                this._manager.audioManager.OpenSound();
                cc.loader.loadRes("startscene/open_music.png", cc.SpriteFrame, (err, spriteFrame) => {
                    soundIcon.spriteFrame = spriteFrame;
                    if (GameConfig.languages == LANG_ENUM.EN) {
                        this.soundLabel.string = `<b> ${lang_zh.sound[GameConfig.languages]} on </b>`;
                    }
                });
            }
        }
    }


}
